package com.dzt.androidframework.photo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.adapter.AbsListAdapter;
import com.dzt.androidframework.photo.ImageModel;

import net.tsz.afinal.FinalBitmap;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by M02323 on 2017/9/18.
 */

public class GridViewAdapter extends AbsListAdapter<ImageModel>{

	private int cbState;
	private FinalBitmap finalBitmap;

	public GridViewAdapter(Context context, List<ImageModel> datas, int cbState){
		super(context, datas);
		this.cbState = cbState;
		finalBitmap = FinalBitmap.create(context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.item_girdview_camera, parent, false);
			x.view().inject(holder, convertView);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		ImageModel imageModel = datas.get(position);
		String path = imageModel.getImagePath(); // 图片文件保存路径
		boolean state = imageModel.isImageState(); // 图片选择状态
		int spareImg = imageModel.getSpareImage(); // 备用显示图片

		if (path.equals("") && spareImg != 0) {
			holder.tvImg.setImageResource(spareImg);
		} else {
			finalBitmap.display(holder.tvImg, path);
			holder.cbBox.setChecked(state);
		}
		holder.cbBox.setVisibility(cbState);
		return convertView;
	}

	class ViewHolder{
		@ViewInject(R.id.iv_gridview)
		ImageView tvImg;
		@ViewInject(R.id.cb_gridview)
		CheckBox cbBox;
	}
}
